package com.lebang.controller.sys.cences;

import com.lebang.common.Login;
import com.lebang.example.CencesApruExample;
import com.lebang.example.CensusExample;
import com.lebang.model.MyConfig;
import com.lebang.service.cences.CensusService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

/**
 * @Author: hwq
 * @Date: 2019/10/9 15:50
 * @Version 1.0
 */
@Controller
@RequestMapping("/cencus")
public class CencusController {
    @Autowired
    private CensusService censusService;

    @GetMapping("/list")
    @Login
    public String list() {
        return "/cencus/list";
    }
    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen( String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end=LocalDate.parse(date, fmt);
        censusService.dayCensus(LocalDateTime.of(end, LocalTime.MIN));
        return "success";
    }


    /**
     * 重置邮件数据
     * @param date
     * @return
     */
    @Login
    @PostMapping("/resetEmailData")
    @ResponseBody
    public String resetEmailData(String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end=LocalDate.parse(date, fmt);
        censusService.emailData(LocalDateTime.of(end, LocalTime.MIN));
        censusService.sendEmail(LocalDateTime.of(end, LocalTime.MIN));
        return "success";
    }


    @Login
    @PostMapping("/del")
    @ResponseBody
    public JsonData del(Integer id) {

        if (censusService.deldayCensus(id) > 0) {
            return Callback.success("success", "操作成功", null);
        } else {
            return Callback.fail("fail", "操作失败", null);
        }
    }
    @Login
    @PostMapping("/list")
    @ResponseBody
    public HashMap list(Long page, Integer limit, Integer star, Integer end) {
        HashMap map = new HashMap(1);
        CensusExample example = new CensusExample();
        CensusExample.Criteria criteria = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("time desc");
        if (star != null && end != null) {
            criteria.andTimeBetween(star, end);
        }
        criteria.andAppIdEqualTo(MyConfig.getEnv());
        map.put("count", censusService.countByExample(example));
        map.put("code", 0);
        map.put("data", censusService.selectByExample(example));
        return map;
    }
    @Login
    @GetMapping("/newlist")
    public String newlist() {
        return "/cencus/newlist";
    }
    @Login
    @PostMapping("/newlist")
    @ResponseBody
    public HashMap newlist(Long page, Integer limit, Integer star, Integer end, String brand) {
        HashMap map = new HashMap(1);
        CencesApruExample example = new CencesApruExample();
        CencesApruExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("time desc");
        if (star != null && end != null) {
            c.andTimeBetween(star, end);
        }
        if (TextUtil.isEmpty(brand)) {
            c.andTypeEqualTo(1);
        } else {
            c.andBrandEqualTo(brand);
            c.andTypeEqualTo(0);
        }

        c.andAppIdEqualTo(MyConfig.getEnv());
        map.put("count", censusService.countApruByExample(example));
        map.put("code", 0);
        map.put("data", censusService.selectApruByExample(example));
        return map;
    }


    @Login
    @PostMapping("/lastCheckApru")
    @ResponseBody
    public String lastCheckApru( String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end=LocalDate.parse(date, fmt);
        censusService.resetApru(LocalDateTime.of(end, LocalTime.MIN));
        return "success";
    }

}
